package com.base.pig.util;


import com.aspose.words.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * @ProjectName intelligent-city
 * @Package com.pig4cloud.pig.lab.util
 * @ClassName Word2PdfTool
 * @Description TODO
 * @Company Lab
 * @Author ZhangZhY
 * @Date 2020/6/1 17:11
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Service
public class Word2PdfTool {
    /**
     * 设置并验证License
     *
     * @return result
     */
    public boolean isLicense() throws IOException {
        boolean result = false;
        ClassPathResource classPathResource = new ClassPathResource("license.xml");
        InputStream licenseXml = classPathResource.getInputStream();

        try {
            License aposeLic = new License();
            aposeLic.setLicense(licenseXml);
            // TODO ignore
            result = true;
            //log.info("Aspose.Words验证License通过...");
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("Aspose.Words验证License失败，导致的原因是：" + e.getMessage());
        }
        return result;
    }


    public void doc2pdf(String Address,String name) throws IOException {

        if (!isLicense()) {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(name);
            //新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(Address);
            //Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换

            long now = System.currentTimeMillis();
           //log.info("转换成功共耗时：" + ((now - old) / 1000.0) + "秒");
            //转化用时
        } catch (Exception e) {
            //log.error("转换失败，失败原因{}",e.getMessage());
            e.printStackTrace();
        }finally {
            os.close();
        }
    }
}
